<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>
<div id="app">
    <el-page-header style="background-color: rgb(0, 149, 120);color: white;line-height: 60px"
            @back="goBack" content="发布商品页面">
    </el-page-header>
    <el-card>
        <el-form>
            <el-form-item label="商品标题" label-width="80px">
                <el-input v-model="product.title"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" label-width="80px">
                <el-input v-model="product.price"></el-input>
            </el-form-item>
            <el-form-item label="商品属性" label-width="80px">
                <el-input v-model="product.attribute"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" label-width="80px">
                <template>
                    <el-select v-model="product.categoryId">
                        <el-option
                                v-for="c in categoryArr"
                                :label="c.name"
                                :value="c.id">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item label="商品图片" label-width="80px">
                <el-upload
                        action="/upload"
                        list-type="picture-card"
                        name="picFile"
                        :on-success="handleSuccess"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
        </el-form>
        <el-button @click="insert()">发布</el-button>
    </el-card>
</div>
</body>
<!-- import Vue before Element -->
<script src="js/vue.js"></script>
<!-- import JavaScript -->
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v=new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                product:{},
                categoryArr:[]
            }
        },
        methods:{
            goBack() {
                console.log('go back');
                history.back();
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                axios.get("/upload/delete?name="+file.response).then(function (){

                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response,file,fileList){
                v.product.url=response;
            },
            insert(){
                if(v.product.url==""||v.product.title==""||v.product.price==""||v.product.attribute==""||v.product.categoryId==""){
                    alert("商品数据不完整！")
                    location.reload();
                }
                axios.post("/product/insert",v.product).then(function (){
                    alert("发布成功！")
                    location.href="/admin.html"
                })
            }
        },
        created:function (){
            axios.get("/category/select").then(function (response){
                v.categoryArr=response.data;
            })
        }
    })
</script>
</html>